var crypto = require('../utils/crypto');
var express = require('express');
var db = require('../dao/db');
var http = require("../utils/http");

var app = express();

//代理商登录
exports.agent_login=function(openid,nickname,sex,headimgurl,unionid,agent,callback){
	
	db.agent_login(openid,nickname,sex,headimgurl,unionid,agent,callback);



};

//创建用户 如果不存在就创建 存在就登录
exports.create_user=function(state,data,callback){
	var coins = 1000;
	var gems = 50;

	var unionid = data.unionid;
	var openid = data.openid;
	var headimgurl = data.headimgurl;
	var nickname = data.nickname;
	var sex = data.sex;
	

	db.is_user_exist(unionid,function(ret){
		//用户不存在 新增并绑定 
		if(!ret){
			//发送http请求创建 昵称 性别 图像 微信unionid
			var param={
				unionid:unionid
			};
			http.get2(url,param,function(result,resp){

			 if(result){
				var array = state.split('_');
				var pid ="";
				var roleId="";
				if(array.length==3){
					 pid = array[0];
					 roleId =array[1];
				}
				console.log("用户绑定 pid = " + pid + "roleId = " +roleId);
				var userId=resp.userId; //用户id
				var userGold=resp.gold;//用户房卡
				db.create_user(userId,openid,unionid,nickname,headimgurl,user_gold,game_id,pid,roleId,function(ret){
					if(ret){
						callback({code:0,msg:'insert sucess'});
					}else{
						callback({code:1,msg:'insert error'});
					}
				  });
				}else{
						callback({code:2,msg:'http error'});
				}
			},false);
		}
		else{

			//判断是否是玩家  如果是玩家 直接退出
			var roleId = ret.role_id;
			if(roleId ==4){
				callback({code:3,msg:'该玩家已经绑定了用户'});
			}
			//修改用户信息 包括 名字 性别 图像等 显示用
			db.update_user_info(account,name,headimgurl,function(ret){
				callback(ret);
			});
		}
	});
};

exports.create_user_player=function(userId,userName,headimgurl,user_gold,gameId,callback){
 //exports.create_user = function(userId,openid,unionid,name,headimg,user_gold,game_id,callback){
	db.create_user(userId,null,null,userName,headimgurl,user_gold,gameId,callback);

};
 
//账号登录
exports.login_userid = function(userId,pwd,callback){

	db.login_userid(userId,pwd,callback);
};

//获取用户信息
exports.getUserInfo=function(id,callback){

	db.get_user_base_info(id,callback);

};

exports.getStaticData=function(id,role_id,callback){
	db.get_static_data(id,role_id,callback);
};

//获取名下代理数据量
exports.getAgentCount=function(id,role_id,callback){

	db.get_agent_count(id,role_id,callback);
};

//获取名下代理详情
exports.getAgentList=function(userId,pageStart,pageLimit,role_id,callback){

	db.get_agent_list(userId,pageStart,pageLimit,role_id,callback);
};

//获取名下玩家数据
exports.getPlayerCount=function(id,role_id,callback){

	db.get_player_count(id,role_id,callback);
};

//获取名下玩家详情
exports.getPlayerList=function(id,pageStart,pageLimit,role_id,callback){

	db.get_player_list(id,pageStart,pageLimit,role_id,callback);
};

//充值
exports.charge=function(data,callback){

	db.charge(data,callback);
};


//设置带来
exports.setAgent=function(data,callback){

	db.set_agent(data,callback);
};
//同步充值数据到游戏服务器
exports.sysncGameGold=function(data,callback){

}

//房卡扣减
exports.updateGoldByUserId=function(userId,num,gameId,callback){


	db.updateGoldByUserId(userId,num,gameId,callback);

};

//绑定用户
exports.bindUser=function(userId,puserId,gameId,callback){

	db.bind_user(userId,puserId,gameId,callback);
};


//绑定用户
exports.getAgentBalance=function(param,callback){

	db.getAgentBalance(param,callback);
};


exports.payAgent=function(param,callback){

	db.payAgent(param,callback);
};

//获取俱乐部列表
exports.getClubList=function(id,role_id,callback){
	db.getClubList(id,role_id,callback);
};

//获取待审核的玩家申请
exports.getClubApplyList=function(club_id,callback){

	db.getClubApplyList(club_id,callback);
};

//审核申请
exports.auditApply = function(apply_id,status,callback){

	db.auditApply(apply_id,status,callback);

};

//获取待审核总数
exports.auditCount=function(agent_id,callback,club_id){
	db.auditCount(agent_id,callback,club_id);
};

//创建俱乐部
exports.createClub=function(params,callback){

	db.createClub(params,callback);
};
//创建俱乐部
exports.getClubUserList=function(params,callback){

	
	db.getClubUserList(params,callback);
};
//删除成员
exports.delteFromClub=function(params,callback){

	
	db.delteFromClub(params,callback);
};
//删除成员
exports.getUserGems=function(params,callback){
	db.getUserGems(params,callback);
};
//修改俱乐部房卡
exports.updateClub=function(params,callback){
	db.updateClub(params,callback);
};
//修改俱乐部房卡
exports.editAgentPay=function(params,callback){
	db.editAgentPay(params,callback);
};
//修改俱乐部房卡
exports.getAgentPayInfo=function(params,callback){
	db.getAgentPayInfo(params,callback);
};
//修改俱乐部房卡
exports.getClubRoom=function(params,callback){
	db.getClubRoom(params,callback);
};
